{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2530f9a9",
   "metadata": {},
   "source": [
    "### 拷贝的三种情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9de5bf68",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "89d4c5e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(12)\n",
    "b = a #这种情况不会进行拷贝\n",
    "print(b is a) #返回True，说明b和a是相同的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6a53a4e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([100,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = a.view()\n",
    "print(c is a)\n",
    "c[0] = 100\n",
    "a\n",
    "# 说明对c上的改变，会影响a上面的值，说明他们指向的内存空间还是一样的，这种叫做浅拷贝，或者说是view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "49de3ec1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "[100 200   2   3   4   5   6   7   8   9  10  11]\n",
      "[100   1   2   3   4   5   6   7   8   9  10  11]\n"
     ]
    }
   ],
   "source": [
    "d = a.copy()\n",
    "print(d is a)\n",
    "d[1] = 200\n",
    "print(d)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1e414c3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 1, 1, 1],\n",
       "       [8, 8, 4, 9]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 之前讲到的flatten和ravel中，ravel返回的就是View，而flatten返回的就是深拷贝\n",
    "a1 = np.random.randint(0,10,size=(2,4))\n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "05108940",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 1, 1],\n",
       "       [8, 8, 4, 9]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2 = a1.ravel()\n",
    "a2[0] = 0\n",
    "a1 # 修改了a1的值，说明ravel返回的是一个浅拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "25dc7109",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0, 100,   1,   1,   8,   8,   4,   9])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3 = a1.flatten()\n",
    "a3[1] = 100\n",
    "a3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e770a73f",
   "metadata": {},
   "source": [
    "# 总结：\n",
    "在数组中操作分为三种拷贝：  \n",
    "1.不拷贝：直接赋值，那么栈区没有拷贝，只是用同一个栈区定义了不同的名称   \n",
    "2.浅拷贝：只拷贝栈区，栈区指定的堆区并没有拷贝   \n",
    "3.深拷贝：栈区和堆区都拷贝了"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
